package net.kts.attraction.subway.smallpass;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


import net.kts.attraction.subway.adapter.SubwayResultAdapter;
import net.kts.attraction.subway.smallpass.model.LastPath;
import net.kts.attraction.subway.smallpass.model.PassLine;
import net.kts.attraction.subway.smallpass.model.PassStation;
import net.kts.attraction.subway.smallpass.model.PathResult;
import net.kts.attraction.subway.smallpass.model.StationName;
import net.kts.attraction.subway.smallpass.process.check.SmallWayChecker;
import net.kts.attraction.subway.smallpass.sqlite.SubwayNameDaoSQLite;
import net.kts.define.SQLiteDatabaseStr;
import net.kts.main.R;
import net.kts.setting.language.LanguageUtil;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

public class SmallSubwayResultActivity extends Activity {

//	private List<StationName> stationNameList = null;
	private SubwayNameDaoSQLite subwayNameDaoSQLite = null;
	private List<PathResult> pathResultList = new ArrayList<PathResult>();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.attaction_subway_result_list);
		
		if(this.subwayNameDaoSQLite != null) {
			this.subwayNameDaoSQLite.close();
		}
		this.subwayNameDaoSQLite = new SubwayNameDaoSQLite(this, SQLiteDatabaseStr.dbFileName, null, 1);
		
		LastPath lastPath = SmallWayChecker.getLastPath();
		
//		int lastCount = lastPath.getPassStationList().size();
//		double lastTime = lastPath.getLastTime();
		
		resultInit(lastPath);
		
		resultStationNameInit();
		
		resultPath(pathResultList, lastPath);
		
//		if(lastPath == null) {
//			Toast.makeText(this, "紐살갼�섎떎!", Toast.LENGTH_SHORT).show();
//		}
//		else {
//			Toast.makeText(this, "李얠븯�� count : " + lastPath.getPassStationList().size(), Toast.LENGTH_SHORT).show();
//		}
		
	}
	
	private void resultStationNameInit() {
		// TODO Auto-generated method stub
		PathResult pathResult = null;
		StationName stationName = null;
		for(int indexI = 0; indexI < this.pathResultList.size(); ++indexI) {
			
			pathResult = this.pathResultList.get(indexI);
			stationName = this.subwayNameDaoSQLite.selectStationName(pathResult.getStationCode(), LanguageUtil.getCurrentLanguage());
			
			pathResult.setStationName(stationName.getStationName());
			
		}
		
	}

	private void resultPath(List<PathResult> pathResultList, LastPath lastPath) {
		
		//�대윭硫��덈릺吏�쭔 ;; 珥덇린 2媛��ｌ뼱二쇨린 �꾪빐 鍮덇컼泥��ｌ뼱以�떎.
		
//		this.pathResultList.add(0, new PathResult());
//		this.pathResultList.add(1, new PathResult());
		//this.pathResultList.add(new PathResult());
		
		
		this.pathResultList.get(0).setLastPath(lastPath);
		
		
		
		SubwayResultAdapter subwayResultAdapter = new SubwayResultAdapter(this, R.layout.attaction_subway_result_list, pathResultList);
		
//		addContentView(this.testFirst(lastPath), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		
		ListView listView = (ListView)findViewById(R.id.attraction_sbuway_result_listview);
		
		TextView txtMinute = (TextView)findViewById(R.id.attractionSubwayResultMinute);
		
		TextView txtCount = (TextView)findViewById(R.id.attractionSubwayResultCount);
		
		txtMinute.setText("Total Lead Time : " + (int)(lastPath.getLastTime() + 1) + " Minute");
    
		txtCount.setText("Total Path Station Count : " + lastPath.getPassStationList().size());
		
		listView.setAdapter(subwayResultAdapter);
		
		SmallSubwayStartActivity.progressDialog.dismiss();
	}
	
	
	
	private void resultInit(LastPath lastPath) {
		
//		StationName stationName = null;
		
		PathResult pathResult = null;
		
		List<PassStation> passStationList = lastPath.getPassStationList();
		List<PassLine> passLineList = lastPath.getPassLineList();
		
		String pettern = "####.##";
		DecimalFormat df = new DecimalFormat(pettern);
		
		
		PassStation passStation = null;
		PassLine passLine = null;
		
		pathResult = new PathResult();
		pathResult.setLineNum(passStationList.get(0).getLineNum());
		pathResult.setStationCode(passStationList.get(0).getStationCode());
		pathResult.setType(1); // 異쒕컻
		
		//異쒕컻����옣
		pathResultList.add(pathResult);
		
		
		int tempLineNum = 0;
		double tempTime = 0;
		for(int indexI = 0; indexI < passStationList.size() ; ++indexI) {
			
			
			passStation = passStationList.get(indexI);
			
			tempTime += passStation.getTime();
			//�꾧퉴 �몄꽑怨��꾩옱 �몄꽑���ㅻ��� -> �섏듅�덈떎��利앷굅
			if(tempLineNum != 0 && passStation.getLineNum() != tempLineNum) {
				
				
				// 癒쇱� �섏듅�����뺣낫 ��옣
				
				pathResult = new PathResult();
				pathResult.setLineNum(passStationList.get(indexI -1).getLineNum());
				pathResult.setStationCode(passStationList.get(indexI -1).getStationCode());
				pathResult.setType(3); // 吏꾪뻾
				pathResult.setPathTime(tempTime);
				
				pathResultList.add(pathResult);
				
				//�섏듅 �뺣낫 媛�졇�ㅺ린
				passLine = this.getTransferTime(passLineList, passStationList.get(indexI -1).getStationCode(), passStation.getLineNum());
				
				// �섏듅���뺣낫 ��옣
				pathResult = new PathResult();
				pathResult.setLineNum(passStation.getLineNum());
				pathResult.setStationCode(passLine.getTargetStationCode());
				pathResult.setType(4); // �섏듅
				pathResult.setPathTime(Double.valueOf(df.format(passLine.getTime())));
				
				pathResultList.add(pathResult);
				
				tempTime = 0;
			}
			tempLineNum = passStation.getLineNum();
			
//			pathResult.setLineNum(passStation.getLineNum());
//			pathResult.setStationCode(passStation.)
			
			
			
		}
		
		passStation = passStationList.get(passStationList.size() -1);
		
		pathResult = new PathResult();
		pathResult.setLineNum(passStation.getLineNum());
		pathResult.setStationCode(passStation.getStationCode());
		pathResult.setPathTime(tempTime);
		pathResult.setType(2); // 異쒕컻
		
		pathResultList.add(pathResult);
		
		
	}
	
	
	private PassLine getTransferTime (List<PassLine> passLineList, int stationCode, int targetLine) {
		
//		double time = 0;
		
		for(PassLine passLine : passLineList) {
			
			if(passLine.getStationCode() == stationCode && passLine.getDirection() == targetLine ) {
				return passLine;
			}
			
		}
		
		
		
		return null;
	}

}
